import {Component, OnInit, Input} from '@angular/core';
import {QuestionTypeService} from './question_type.service';
import { NzMessageService } from 'ng-zorro-antd';
import {QuestionTypeModel, Test} from './model/questionTypeModel';
import {isUndefined} from "util";

@Component({
  selector: 'app-question-type',
  templateUrl: 'question_type.component.html',
  styleUrls: ['./question_type.component.less'],
  providers: [QuestionTypeService]
})

export class QuestionTypeComponent implements OnInit{
  size = 'Large';
  inputValue: string;
  deleteIcon = false;
  disShow = false;
  addtype = false;
  quetionTypeAll: QuestionTypeModel;
  questionKindqList: Test[];
  showqkid: string;


  constructor(private message: NzMessageService, private questionTypeService: QuestionTypeService) {

  }
  ngOnInit() {
    this.queryList();
  }
  /*查询类目*/
  queryList() {
    this.questionTypeService.queryAll()
      .then((result: any) => {
        this.questionKindqList = result;
        const showid = this;
        this.questionKindqList.forEach(function (item) {
          if(item.kindName === "默认题型") {
            showid.showqkid = item.qkid;
          }
        })
      });
  }
  /* 删除所有类目 */
  deleteTypeOne() {
    this.deleteIcon = true;
  }
  deleteTypeALL() {
    if(this.questionKindqList.length === 0 || this.addtype === true || this.disShow === true) {
      this.deleteIcon = false;
    }else {
      this.deleteIcon = true;
    }

  }
  /* 删除单个类目 */
  cancel = function () {
    this.message.info('click cancel');
  }
  confirm = (qkid: string) => {
    this.questionTypeService.remove(qkid).then(()=>{
      this.queryList();
    });
    this.deleteIcon = false;
  }
  undelete() {
    this.deleteIcon = false;
  }

  /*新增类目*/
  addType() {
    this.inputValue = undefined;
    if(this.deleteIcon == true || this.disShow === true){
      this.addtype = false;
    } else {
      this.addtype = true;
    }
  }
  newSaveType() {
    if(this.inputValue === undefined){
      this.message.info('添加内容不能为空');
    }else {
      this.questionTypeService.create({kindName: this.inputValue})
        .then((result: Test) => {
          this.queryList();
        });
      this.addtype = false;
    }
  }

  /* 编辑类目 */
  editALL() {
    // debugger this.questionKindqList.length === 0 ||
    if( this.questionKindqList.length === 0 || this.deleteIcon === true || this.addtype === true) {
      this.disShow = false;
    } else {
      this.disShow = true;
      this.addtype = false;
    }
  }
  saveType(qkid: string, kindName: string) {
  }
  //编辑完成
  editALLEnd() {
    const editqkid = this;
    let _status = false;
    this.questionKindqList.forEach(function (item) {
      console.log(item);
      if(item.qkid === editqkid.showqkid && item.kindName != "默认题型") {
        editqkid.message.info('默认题型不能修改！');
        _status = true;
      }
    })
    if(!_status){
      this.questionTypeService.QuestionEdit({questionKinds: this.questionKindqList})
        .then((result: any) => {
          this.queryList();
          this.disShow = false;
        });
    }
  }

  /*取消类目*/
  cancelType() {
    this.addtype = false;
  }
}


